package com.kgc.ubatnet.ubatnetuserweb.controller;

import com.kgc.ubatnet.bean.PmsBaseAttrInfo;
import com.kgc.ubatnet.bean.PmsBaseAttrValue;
import com.kgc.ubatnet.service.AttrService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class AttrController {

    @Reference
    AttrService attrService;
    @RequestMapping("/seleAttrByCata3")
    public List<PmsBaseAttrInfo> attrInfoList(Long catalog3Id){
        List<PmsBaseAttrInfo> infoList = attrService.select(catalog3Id);
        return infoList;
    }

    //添加平台属性
    @RequestMapping("/addAttrInfo")
    public String addAttrInfo(@RequestParam("attrName")String attrName,
                              @RequestParam("catalog3Id")Long catalog3Id,
                              @RequestParam("pmsBaseAttrValueList")List<String> pmsBaseAttrValueList){
        PmsBaseAttrInfo pmsBaseAttrInfo=new PmsBaseAttrInfo();
        pmsBaseAttrInfo.setAttrName(attrName);
        pmsBaseAttrInfo.setCatalog3Id(catalog3Id);
        List<PmsBaseAttrValue> pmsBaseAttrValueLists=new ArrayList<>();
        for (String s : pmsBaseAttrValueList) {
            PmsBaseAttrValue pmsBaseAttrValue=new PmsBaseAttrValue();
            pmsBaseAttrValue.setValueName(s);
            pmsBaseAttrValueLists.add(pmsBaseAttrValue);
        }
        pmsBaseAttrInfo.setPmsBaseAttrValueList(pmsBaseAttrValueLists);
        Integer add = attrService.add(pmsBaseAttrInfo);
        if(add>0){
            return "success";
        }else{
            return "fail";
        }
    }

    //根据平台属性查询值
    @RequestMapping("/getAttrValueList")
    public PmsBaseAttrInfo getAttrValueList(Long attrId){
        PmsBaseAttrInfo attrValueList = attrService.getAttrValueList(attrId);
        return attrValueList;
    }

    //修改平台属性属性值
    @RequestMapping("/upAttrval")
    public String upAttrval(@RequestParam("pmsBaseAttrValueList")List<String> pmsBaseAttrValueList,
                            @RequestParam("attrName")String attrName,
                            @RequestParam("catalog3Id")String catalog3Id,
                            @RequestParam("id")Long id){
        PmsBaseAttrInfo pmsBaseAttrInfo=new PmsBaseAttrInfo();
        pmsBaseAttrInfo.setId(id);
        pmsBaseAttrInfo.setAttrName(attrName);
        pmsBaseAttrInfo.setCatalog3Id(Long.parseLong(catalog3Id));
        List<PmsBaseAttrValue> pmsBaseAttrValueLists=new ArrayList<>();
        for (String s : pmsBaseAttrValueList) {
            PmsBaseAttrValue pmsBaseAttrValue=new PmsBaseAttrValue();
            pmsBaseAttrValue.setValueName(s);
            pmsBaseAttrValueLists.add(pmsBaseAttrValue);
        }
        pmsBaseAttrInfo.setPmsBaseAttrValueList(pmsBaseAttrValueLists);
        Integer add = attrService.add(pmsBaseAttrInfo);
        if(add>0){
            return "success";
        }else{
            return "fail";
        }
    }


    //删除平台属性
    @RequestMapping("/deleAttr")
    public String deleAttr(Long attrid){
        Integer integer = attrService.deleAttr(attrid);
        System.out.println(integer);
        if(integer>0){
            return "success";
        }else{
            return "fail";
        }
    }

}
